import requests
from bs4 import BeautifulSoup
import re
import csv
url = 'https://desk.zol.com.cn/meinv/'

head = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.56'
}
data = requests.get(url, headers=head)
data.encoding = 'gb2312'

soup = BeautifulSoup(data.text, 'html.parser')
data.close()

data = soup.find("ul", class_=["pic-list2", "clearfix"])

table =data.find_all("a", class_="pic")

# 高清图片地址
# print('https://desk.zol.com.cn'+table[1].get('href'))
# 获取缩图片
# print(table[1].img)
# 简写
# print(soup.select('img'))

i=0
for x in table:
  htmlurl ='https://desk.zol.com.cn'+ table[i].get('href')
  f_name =htmlurl.rsplit(".")[-1]
  i=i+1
  # 判断是不是图片地址，这里防止下载到exe广告
  if f_name =="html":

    # imgpage = table[i].span.text
    # imgpagenum = int(re.findall(r"\d", imgpage)[0])
    imgdata = requests.get(htmlurl)
    # imgpage =htmlurl.split('_')
    #
    # print(imgpage)
    # imgpagenum1 =int(imgpage[1])
    # num = imgpagenum1-imgpagenum
    # if num < imgpagenum1:
    #   pageurl = imgpage[0]+'_'+str(num)+'_'+imgpage[2]
    #   num=num+1
    # 获取图库中其他图片，因为图片地址根据imgpage的数量改变 可能比imgpage 多 可能比imgpage少 所以要拼接url判断是否404
    # 获取其他图片的地址
    #   print(pageurl)
    imgsoup = BeautifulSoup(imgdata.text, 'html.parser')
    imgdata.close()
    imgurl = imgsoup.find("img", id="bigImg").get('src')
    img_dowloads = requests.get(imgurl)
    img_name = imgurl.split("/")[-1]
    print(img_name)
    with open("./bigimg/" + img_name, mode="wb") as f:
        f.write(img_dowloads.content)





